﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using StoragePeriod.BLL.FoodTesting;
using StoragePeriod.Model.FoodTesting;
using StoragePeriod.Web.Common;
using StoragePeriod.BLL.RawDict;
using StoragePeriod.Model.RawDict;

namespace StoragePeriod.Web.FoodTesting.VAFood
{
    public partial class VAFoodEdit : System.Web.UI.Page
    {
        T_FoodRecord tFoodRecord = new T_FoodRecord();  //检测记录
        T_VAData tVAData = new T_VAData();              //VA检测记录
        T_RawMaterial tRawMaterial = new T_RawMaterial();//原料操作

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["RawID"] != null && Request.QueryString["FoodTime"] != null && Request.QueryString["FoodNum"] != null)
                {
                    this.LoadData();
                }
            }
        }

        //加载数据
        public void LoadData()
        {
            DateTime date = Convert.ToDateTime(Request.QueryString["FoodTime"].ToString());
            int RawId = Convert.ToInt32(Request.QueryString["RawID"].ToString());
            List<E_VAData> eVADataList = tVAData.GetModelList("RawID=" + RawId + " and FoodTime=cast('" + date.ToString() + "' as datetime)");

            E_FoodRecord md_FoodRecord = new E_FoodRecord();
            md_FoodRecord.FoodTypeID = 6;//Vc检测
            md_FoodRecord.FoodTime = date;
            md_FoodRecord.RawID = RawId;
            E_FoodRecord eFoodRecord = tFoodRecord.GetFoodRecord(md_FoodRecord);

            if (eFoodRecord != null)
            {
                this.labMean.Text = eFoodRecord.FoodResult;
            }

            if (eVADataList != null)
            {
                int FoodNum = int.Parse(Request.QueryString["FoodNum"]);
                E_VAData eVcData = eVADataList.Where(p => p.FoodNum == FoodNum).FirstOrDefault();//获取第一次检测记录
                if (eVcData != null)
                {
                    this.txt_Result.Text = eVcData.Result.ToString();
                }
            }
        }

        //提交数据
        protected void btPost_Click(object sender, EventArgs e)
        {
            int RawId = Convert.ToInt32(Request.QueryString["RawID"]);
            DateTime date = Convert.ToDateTime(Request.QueryString["FoodTime"]);
            int FoodNum = int.Parse(Request.QueryString["FoodNum"]);
            try
            {
                double Result = Convert.ToDouble(this.txt_Result.Text.Trim());
                E_RawMaterial eRawMaterial = tRawMaterial.GetModel(RawId);

                E_VAData eVAData = new E_VAData();
                eVAData.RawID = RawId;
                eVAData.RawName = eRawMaterial.RAWName;
                eVAData.Result = Result.ToString("f2");
                eVAData.FoodTime = date;
                eVAData.FoodNum = FoodNum;

                tVAData.RemoveByFoodNum(eVAData);   //删除现有的检测记录
                tVAData.Add(eVAData);               //添加检测记录
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('提交成功！');</script>");
            }
            catch
            {
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('输入有误！');</script>");
            }
        }

        //计算平均值
        protected void btnMean_Click(object sender, EventArgs e)
        {
            DateTime date = Convert.ToDateTime(Request.QueryString["FoodTime"].ToString());
            int RawId = Convert.ToInt32(Request.QueryString["RawID"].ToString());
            List<E_VAData> eVADataList = tVAData.GetModelList("RawId=" + RawId + " and FoodTime=cast('" + date.ToString() + "' as datetime)");


            E_FoodRecord md_FoodRecord = new E_FoodRecord();
            md_FoodRecord.FoodTypeID = 6;//Vc检测
            md_FoodRecord.FoodTime = date;
            md_FoodRecord.RawID = RawId;
            E_FoodRecord eFoodRecord = tFoodRecord.GetFoodRecord(md_FoodRecord);

            if (eVADataList == null || eVADataList.Count < 1)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('请先提交检测数据！');</script>");
                return;
            }

            double Result = 0;//检测结果
            eVADataList.ForEach(p =>
            {
                Result += Convert.ToDouble(p.Result);
            });

            if (eFoodRecord != null)  //若存在则进行修改
            {
                eFoodRecord.FoodResult = (Result / eVADataList.Count).ToString("f2");
                tFoodRecord.Update(eFoodRecord);
                this.labMean.Text = eFoodRecord.FoodResult;
            }
            else
            {
                md_FoodRecord.FoodResult = (Result / eVADataList.Count).ToString("f2");
                E_RawMaterial eRawMaterial = tRawMaterial.GetModel(RawId);
                md_FoodRecord.RawName = eRawMaterial.RAWName;
                tFoodRecord.Add(md_FoodRecord);
                this.labMean.Text = md_FoodRecord.FoodResult;
            }
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('计算完毕！');parent.window.RefValue();</script>");
        }
    }
}